package io.jee.medusa.region;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RegionHelper {
	
	public static List<Region> DATA;
	public static List<Region> DATA_PARENT;
	
	private static RegionHelper helper;
	
	@SuppressWarnings("unchecked")
	public static RegionHelper getInstance(){
		if(helper==null){
			helper = new RegionHelper();
			try {
				ObjectInputStream ois = new ObjectInputStream(RegionHelper.class.getResourceAsStream("/region.data"));
				DATA = (List<Region>) ois.readObject();
				if(ois!=null){
					ois.close();
				}
			} catch (IOException | ClassNotFoundException e) {
				e.printStackTrace();
			}
			DATA_PARENT = new ArrayList<>();
			Iterator<Region> iterator = DATA.iterator();
			while (iterator.hasNext()) {
				Region region = iterator.next();
				if(region.getParent()==null){
					DATA_PARENT.add(region);
				}
			}
		}
		return helper;
	}
	
	public Region getRegion(int id){
		Iterator<Region> iterator = DATA.iterator();
		while (iterator.hasNext()) {
			Region region = iterator.next();
			if(region.getId().equals(id)){
				return region;
			}
		}
		return null;
	}
	
	public List<Region> getRegionParent(){
		return DATA_PARENT;
	}

}
